<?php


namespace app\common\model;


use think\Db;
use think\Model;

class UserWithdrawModel extends Model
{
    protected $name = 'user_withdraw';

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [
        'status_text',
    ];



    /**
     * 提现申请
     *
     * @param $user_id int 用户ID
     * @param $withdraw_money double 提现金额
     * @param $name string 姓名
     * @param $account string 提现账号/卡号
     * @param $bank string 开户行
     * @param $memo string 用户备注
     */
    public function applyWithdraw($params, $user_id)
    {
        extract($params);

        Db::startTrans();

        try {
            $system = model('app\common\model\SystemModel')->get(1);
            $withdraw_limit = $system->withdraw_limit;
            $withdraw_service_charge = $system->withdraw_service_charge;
            $user = model('app\common\model\User')->get($user_id);
            if($withdraw_limit > $withdraw_money){
                throw new \Exception('提现金额小于最低额度',47201);
            }
            if($user->money < $withdraw_money){
                throw new \Exception('余额不足',47202);
            }
            $service_charge = 0;
            $amount = $withdraw_money;
            if($withdraw_service_charge > 0) {
                $service_charge = $withdraw_money * ($withdraw_service_charge / 100);
                $amount = $withdraw_money - $service_charge;
            }
            $create = [
                'user_id' => $user_id,
                'account' => $account??'',
                'bank' => $bank??'',
                'name' => $name??'',
                'mobile' => $mobile??'',
                'money' => $withdraw_money,
                'service_charge' => $service_charge,
                'amount' => $amount,
                'memo' => $memo??'',
                'status' => 0,
            ];
            model('app\common\model\UserWithdrawModel')->create($create);

            \app\common\model\User::money(-$withdraw_money, $user_id,'提现申请');
            \app\common\model\User::where(['id' => $user_id])->setInc('freezing_money',$withdraw_money);

            Db::commit();
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => []
            ];
        }catch (\Exception $e) {
            Db::rollback();
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => $e->getLine()
            ];
        }
    }


    /**
     * 提现申请记录
     *
     * @param int $user_id 用户ID
     * @param string $mold 状态:-1=全部,0=待审核,1=审核通过,2=审核拒绝
     * @param int $page 页码
     * @param int $limit 每页数据
     */
    public function getPageList($user_id,$mold,$page,$limit)
    {
        try {
            $where = [
                'user_id' => $user_id,
            ];
            if($mold > -1) {
                $where['status'] = $mold;
            }
            $list = model('app\common\model\UserWithdrawModel')
                ->where($where)
                ->order('createtime', 'desc')
                ->paginate($limit, false, ['page' => $page]);
            foreach ($list as $row) {
                $row->createtime = datetime($row->createtime,'Y-m-d');
                $row->updatetime = datetime($row->updatetime,'Y-m-d');
            }
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $list
            ];
        }catch (\Exception $e) {
            Db::rollback();
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => $e->getLine()
            ];
        }
    }




    public function getStatusList()
    {
        return ['0' => __('待审核'), '1' => __('审核通过'), '2' => __('审核拒绝')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

}